package com.nanmeishu.services.user.controller;


import com.nanmeishu.infrastructure.web.entity.ResponseResult;
import com.nanmeishu.infrastructure.web.util.ResultUtil;
import com.nanmeishu.services.user.dto.LoginDto;
import com.nanmeishu.services.user.dto.UpdatePasDto;
import com.nanmeishu.services.user.entity.User;
import com.nanmeishu.services.user.facade.FriendService;
import com.nanmeishu.services.user.facade.UserService;
import com.nanmeishu.services.user.vo.CountUserVo;
import com.nanmeishu.services.user.vo.GetUserProgressBarsVo;
import com.nanmeishu.web.TokenVerifyAnnotation;
import io.swagger.annotations.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.*;
import javax.servlet.http.HttpServletRequest;
import javax.validation.Valid;

@Api(tags = "用户接口")
@RestController
@RequestMapping("/sys/user")
public class UserController {

    @Autowired
    UserService userService;

    @Autowired
    FriendService friendService;


    @Value("${TOKEN.DATE}")
    String TOKEN_DATE;

    @PostMapping("/login")
    @ApiOperation("用戶登录接口")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "username", value = "用户名", paramType = "body", required = true),
            @ApiImplicitParam(name = "password", value = "密码（MD5加密）", paramType = "body", required = true)
    })
    @ApiResponses({
            @ApiResponse(code = 200, message = "令牌token", response = String.class)
    })
    public ResponseResult login(@RequestBody LoginDto loginDto) {
        String token = userService.login(loginDto);
        return ResultUtil.success(token);
    }

    @ApiOperation("注册接口")
    @PostMapping("/register")
    public ResponseResult register(@RequestBody @Valid User user) {
        userService.register(user);
        return ResultUtil.success();
    }

    @ApiOperation("修改用户信息")
    @PostMapping("/update")
    @TokenVerifyAnnotation
    public ResponseResult update(@RequestBody User user, HttpServletRequest request) {
        userService.updateUser(user,request);
        return ResultUtil.success();
    }

    @ApiOperation("修改密码")
    @PostMapping("/updatePas")
    @TokenVerifyAnnotation
    public ResponseResult updatePas(@RequestBody UpdatePasDto updatePasDto, HttpServletRequest request) {
        userService.updatePas(updatePasDto,request);
        return ResultUtil.success();
    }


    @ApiOperation("找回密码")
    @PostMapping("/getBackPas")
    public ResponseResult getBackPas(@RequestBody UpdatePasDto updatePasDto) {
        userService.getBackPas(updatePasDto);
        return ResultUtil.success();
    }

    @ApiOperation("通过id拿到用户对象")
    @GetMapping("/get")
    @ApiImplicitParam(name = "userId", value = "用户id", required = true)
    @ApiResponses({
            @ApiResponse(code = 200, message = "用户对象", response = User.class)
    })
    public ResponseResult get(@RequestParam("userId") String userId) {
        return ResultUtil.success(userService.getById(userId));
    }

//    @GetMapping("/verifyToken")
//    public ResponseResult verifyToken(@RequestParam("token") String token) {
//        Jedis jedis = null;
//        try {
//            jedis = redisUtil.getJedis();
//            if (jedis.get(token) != null) {
//                //token存在
//
//                if (JwtUtil.verify(token)) {
//                    return ResultUtil.success(true);
//                }
//            }
//            return ResultUtil.success(false);
//        } finally {
//            jedis.close();
//        }
//    }

    @ApiOperation("通过token拿到用户信息记录")
    @TokenVerifyAnnotation
    @GetMapping("/getUserByToken")
    public ResponseResult getUserByToken(HttpServletRequest request) {
        User user= userService.getUserByToken(request);
        return ResultUtil.success(user);
    }

    @ApiOperation("返回用户人生进度条数据")
//    @TokenVerifyAnnotation
    @GetMapping("/getUserProgressBars")
    public ResponseResult getUserProgressBars(HttpServletRequest request) {
        GetUserProgressBarsVo getUserProgressBarsVo = userService.getUserProgressBars(request);
        return ResultUtil.success(getUserProgressBarsVo);
    }

    @ApiOperation("返回用户的资源数量")
    @TokenVerifyAnnotation
    @GetMapping("/countUser")
    public ResponseResult countUser(HttpServletRequest request) {
        CountUserVo countUserVo=userService.countUser(request);
        return ResultUtil.success(countUserVo);
    }

}
